﻿using Core.SimpleTemp.Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Core.Toolkit.Startup
{
    public static class ToolkitApplicationBuilderExtensions
    {

        public static IApplicationBuilder UseToolkitExceptionHandler(this IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //生产环境异常处理
                app.UseExceptionHandler(new ExceptionHandlerOptions()
                {
                    ExceptionHandler = async context =>
                    {
                        context.Response.StatusCode = 500;
                        context.Response.ContentType = "application/json;charset=utf-8";
                        var err = context.Features.Get<IExceptionHandlerFeature>();
                        var jsonErrorResponse = context.Response.CreateAjaxResponseExceptionJson(err.Error, env.IsDevelopment(), context.TraceIdentifier);
                        await context.Response.WriteAsync(jsonErrorResponse);
                    }
                });
            }
            return app;
        }


        public static IApplicationBuilder UseToolkitStaticFiles(this IApplicationBuilder app, IHostingEnvironment env, WebAppOptions webAppOptions)
        {

            var cachePeriod = env.IsDevelopment() ? "600" : "604800";//七天
            //wwwroot
            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
                },
            });
            //整个项目目录
            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
                },
                FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
            });
            //临时文件目录
            if (!System.IO.Directory.Exists(webAppOptions.TempDirModel.TempDir))
                System.IO.Directory.CreateDirectory(webAppOptions.TempDirModel.TempDir);
            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
                },
                FileProvider = new PhysicalFileProvider(webAppOptions.TempDirModel.TempDir)
            });

            //附件目录
            if (!System.IO.Directory.Exists(webAppOptions.FileSaveBasePath))
                System.IO.Directory.CreateDirectory(webAppOptions.FileSaveBasePath);
            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
                },
                FileProvider = new PhysicalFileProvider(webAppOptions.FileSaveBasePath)
            });

            return app;
        }

        public static IApplicationBuilder UseToolkit(this IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime, WebAppOptions webAppOptions)
        {
            app.UseToolkitExceptionHandler(env);
            app.UseToolkitStaticFiles(env, webAppOptions);
            return app;
        }
    }
}
